package com.ns.scs.props.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ns.core.annotations.ReqLog;
import com.ns.core.bo.IdsBO;
import com.ns.core.sentinel.LimitType;
import com.ns.core.sentinel.RateLimiter;
import com.ns.core.utils.StpUtil;
import com.ns.core.utils.sg.ResultOk;
import com.ns.scs.props.bo.PropBO;
import com.ns.scs.props.bo.PropPageBO;
import com.ns.scs.props.dto.PropPageDto;
import com.ns.scs.props.entry.PropEntry;
import com.ns.scs.props.service.IPropService;
import com.ns.scs.props.vo.PropEntryVO;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 参数相关操作
 *
 * @author lw
 */
@RestController
@RequestMapping("/prop/")
@RequiredArgsConstructor
@CrossOrigin
public class PropController {

    private final IPropService propService;

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("add")
    @ReqLog(module = "参数模块", name = "增")
    public ResultOk add(@RequestBody PropBO bo) {
        PropEntry propEntry = new PropEntry();
        BeanUtils.copyProperties(bo, propEntry);
        propEntry.setSort(propService.count() + 1);
        return ResultOk.getByBoolean(propService.save(propEntry));
    }

    /**
     * 改
     *
     * @param bo 配置
     */
    @PostMapping("update")
    @ReqLog(module = "参数模块", name = "改")
    public ResultOk update(@RequestBody PropBO bo) {
        PropEntry propEntry = new PropEntry();
        BeanUtils.copyProperties(bo, propEntry);
        return ResultOk.getByBoolean(propService.updateById(propEntry));
    }

    /**
     * 改状态
     *
     * @param id     id
     * @param status 状态
     */
    @GetMapping("updateStatus")
    @ReqLog(module = "参数模块", name = "改状态")
    public ResultOk updateStatus(Long id, Integer status) {
        PropEntry propEntry = new PropEntry();
        propEntry.setId(id);
        propEntry.setStatus(status);
        propEntry.setModifier(StpUtil.getLoginUserId());
        return ResultOk.getByBoolean(propService.updateById(propEntry));
    }

    /**
     * 逻辑删
     *
     * @param id 参数
     */
    @GetMapping("delete")
    @ReqLog(module = "参数模块", name = "逻辑删")
    ResultOk delete(Long id) {
        return ResultOk.getByBoolean(propService.removeById(id));
    }

    /**
     * 删 - 根据id列表
     */
    @PostMapping("deleteByIds")
    @ReqLog(module = "参数模块", name = "批量删")
    public ResultOk deleteByIds(@RequestBody IdsBO bo) {
        return ResultOk.getByBoolean(propService.removeBatchByIds(bo.getIds()));
    }

    /**
     * 查询详情
     *
     * @param id 参数
     */
    @GetMapping("getById")
    @ReqLog(module = "参数模块", name = "详情")
    public ResultOk get(Long id) {
        PropEntry propEntry = propService.getById(id);
        PropEntryVO peo = new PropEntryVO();
        BeanUtils.copyProperties(propEntry, peo);
        return ResultOk.getSuccessData(peo);
    }

    /**
     * 查-集合
     *
     * @param bo 参数
     */
    @PostMapping("getList")
    @ReqLog(module = "参数模块", name = "查-集合")
    @RateLimiter(time = 5, count = 5, limitType = LimitType.IP)
    public ResultOk getList(@RequestBody PropPageBO bo) {
        PropPageDto dto = new PropPageDto();
        BeanUtils.copyProperties(bo, dto);
        dto.setSid(StpUtil.getSid());
        Page<PropEntryVO> page = propService.getList(dto);
        return ResultOk.getPageData(page.getTotal(), page.getRecords());
    }

    /**
     * 查-类别编码查询
     *
     * @param clazzCode 参数
     */
    @GetMapping("getByClazzCode")
    @ReqLog(module = "参数模块", name = "查-类别编码查询", authFlag = false)
    @RateLimiter(time = 5, count = 5, limitType = LimitType.IP)
    public ResultOk getByClazzCode(String clazzCode) {
        return ResultOk.getSuccessData(propService.getByClazzCode(clazzCode));
    }
}
